package com.work.keep.refresh;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
 * Created by dllo on 16/1/17.
 */
public abstract class StateLayoutManager extends FrameLayout implements LayoutShowLoadingState {
    //容器布局
    private View container;
    //当前状态
    private LoadState curState = LoadState.NONE;
    //前一个状态
    private LoadState preState = LoadState.NONE;

    public View getContainer() {
        return container;
    }

    public void setContainer(View container) {
        this.container = container;
    }

    public LoadState getCurState() {
        return curState;
    }

    public void setCurState(LoadState curState) {
        this.curState = curState;
    }

    public LoadState getPreState() {
        return preState;
    }

    public void setPreState(LoadState preState) {
        this.preState = preState;
    }

    public StateLayoutManager(Context context) {
        this(context, null);
    }

    public StateLayoutManager(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StateLayoutManager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    //初始化容器布局
    private void init(Context context, AttributeSet attrs) {
        container = createContainView(context, attrs);
        if (container != null) {
            throw new NullPointerException("容器布局弄错了把");
        }
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        addView(container, params);
    }

    //容器的创建 初始化方法
    protected abstract View createContainView(Context context, AttributeSet attrs);

    public void show(boolean show) {
        ViewGroup.LayoutParams params = container.getLayoutParams();
        //如果当前的状态 正是我们所期望的状态 那么不作任何改变
        if (show == (getVisibility() == VISIBLE)) {
            return;
        }
        params.height = show ? ViewGroup.LayoutParams.WRAP_CONTENT : 0;
        setVisibility(show ? VISIBLE : INVISIBLE);
    }

    @Override
    public void setLoadState(LoadState state) {
        if (curState != state) {
            preState = curState;
            curState = state;
            onStateChanged(curState, preState);
        }
    }

    protected void onStateChanged(LoadState curState, LoadState preState) {
        switch (curState) {
            case RESET:
                onReset();
                break;
            case RELEASE_TO_REFRESH:
                onReadyForReleaseToRefresh();
                break;
            case PULL_TO_REFRESH:
                onPullToRefresh();
                break;
            case REFRESHING:
                onRefreshing();
                break;
            case NO_MORE_DATA:
                onNoMoreData();
                break;
            default:
                break;
        }

    }

    protected abstract void onNoMoreData();

    protected abstract void onRefreshing();

    protected abstract void onPullToRefresh();

    protected abstract void onReadyForReleaseToRefresh();

    protected abstract void onReset();


    @Override
    public void getLoadState() {

    }

    //------------------------------------------//
    //设置文字内容

    /**
     * 设置最后更新的时间文本
     *
     * @param label 文本
     */
    public void setLastUpdatedLabel(CharSequence label) {

    }

    /**
     * 设置加载中的图片
     *
     * @param drawable 图片
     */
    public void setLoadingDrawable(Drawable drawable) {

    }

    /**
     * 设置拉动的文本，典型的是“下拉可以刷新”
     *
     * @param pullLabel 拉动的文本
     */
    public void setPullLabel(CharSequence pullLabel) {

    }

    /**
     * 设置正在刷新的文本，典型的是“正在刷新”
     *
     * @param refreshingLabel 刷新文本
     */
    public void setRefreshingLabel(CharSequence refreshingLabel) {

    }

    /**
     * 设置释放的文本，典型的是“松开可以刷新”
     *
     * @param releaseLabel 释放文本
     */
    public void setReleaseLabel(CharSequence releaseLabel) {

    }

    public abstract int getContentSize();

    @Override
    public void onPull(float scale) {

    }
}
